package com.vincent.kdp.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.vincent.baseapp.util.ScreenUtil;
import com.vincent.kdp.R;
import com.vincent.kdp.ui.bean.entity.LikeCommodity;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/6/13 0013.
 */
public class LikeCommoditiesAdapter extends RecyclerView.Adapter<LikeCommoditiesAdapter.ViewHolder> {

    private Context context;
    private OnCheckedItemClicker onCheckedItemClicker;
    public int newCheckedIndex = 0;
    private List<LikeCommodity> mLikeCommodityList;

    public LikeCommoditiesAdapter(Context context, List<LikeCommodity> likeCommodityList, OnCheckedItemClicker
            onCheckedItemClicker) {
        this.context = context;
        this.onCheckedItemClicker = onCheckedItemClicker;
        mLikeCommodityList = likeCommodityList;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if(mLikeCommodityList.get(position)!=null){
            holder.mCommodityNameTv.setText(mLikeCommodityList.get(position).getStyleNum());
            Glide.with(context).load(mLikeCommodityList.get(position).getImageUrl()).placeholder(R.mipmap
                    .ic_default_image).into(holder.mCommodityImg);
        }else {
            holder.mCommodityNameTv.setText("");
            Glide.with(context).load("").placeholder(R.mipmap
                    .ic_default_image).into(holder.mCommodityImg);
        }


    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rl_commodity_like, parent, false);
        ViewGroup.LayoutParams params = view.getLayoutParams();
        if (getItemCount() <= 3) {
            params.width = getScreenWidth() / getItemCount();
        }
        view.setLayoutParams(params);
        return new ViewHolder(view, onCheckedItemClicker);
    }


    @Override
    public int getItemCount() {
        return mLikeCommodityList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        private OnCheckedItemClicker onCheckedItemClicker;

        @Bind(R.id.commodity_img)
        ImageView mCommodityImg;
        @Bind(R.id.commodity_name_tv)
        TextView mCommodityNameTv;


        public ViewHolder(View itemView, OnCheckedItemClicker onCheckedItemClicker) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            itemView.setOnClickListener(this);
            this.onCheckedItemClicker = onCheckedItemClicker;
        }

        @Override
        public void onClick(View v) {
            if (onCheckedItemClicker != null) {
                if (getAdapterPosition() != -1) {
                    newCheckedIndex = getAdapterPosition();
                    onCheckedItemClicker.onItemChecked(v, getAdapterPosition(), getItemCount());
                }
            }
        }
    }

    private int getScreenWidth() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        return displayMetrics.widthPixels - ScreenUtil.getInstance(context).dip2px(20);
    }

    public interface OnCheckedItemClicker {
        void onItemChecked(View view, int newPosition, int itemCount);
    }
}
